package demo.thread.pool2;

import java.util.ArrayList;
import java.util.List;

public class TaskThreadPoolImpl implements ThreadPool {

	private int threadNumber = Runtime.getRuntime().availableProcessors();

	private volatile List<TaskThread> taskThreads = new ArrayList<TaskThread>(
			threadNumber);

	public TaskThreadPoolImpl() {
		for (int i = 0; i < threadNumber; i++) {
			taskThreads.add(new TaskThread());
		}
	}

	public synchronized TaskThread getTaskThread() {
		if (taskThreads.size() <= 0)
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		return taskThreads.get(0);
	}

	public synchronized void setTaskThread(TaskThread taskThread) {
		if (taskThreads.size() > threadNumber)
			return;
		taskThreads.add(taskThread);
		this.notify();
	}
}
